local skel = fk.CreateSkill {
  name = "lb__choumou",
}

local DIY = require "packages.diy_utility.diy_utility"

Fk:loadTranslationTable {
  ["lb__choumou"] = "绸缪",
  --[":lb__choumou"] = "弃牌阶段开始时，你可以摸数量等同于本回合进入弃牌堆的类别数张牌，若如此做，你将本阶段弃置的牌置于武将牌上，称为「<b>奇兵</b>」，然后弃置花色数唯一最多的「<b>奇兵</b>」。",
  --10.6
  [":lb__choumou"] = "弃牌阶段开始时，你可以摸数量等同于本回合进入弃牌堆的花色数张牌，若如此做，你可以将本阶段弃置的任意张牌置于武将牌上，称为「<b>奇兵</b>」，若大于一张，你弃置花色数唯一最多的「<b>奇兵</b>」。",

  ["#lb__choumou"] = "绸缪：你可以摸%arg张牌，令本阶段你弃置的牌置于「<b>奇兵</b>」",
  ["#lb__choumou_put"] = "绸缪：你可以将任意张牌置于「<b>奇兵</b>」",
  ["lb__choumou_qibing"] = "奇兵",

  ["$lb__choumou1"] = "该出奇兵了。",
  ["$lb__choumou2"] = "看来是棋逢对手。",
}

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and player.phase == Player.Discard then
      local list = {}
      for _, id in ipairs(DIY.getCenterCards(player.room)) do
        table.insertIfNeed(list, Fk:getCardById(id).suit)
      end
      if #list > 0 then
        event:setCostData(self, { num = #list })
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player,
      { skill_name = skel.name, prompt = "#lb__choumou:::" .. event:getCostData(self).num })
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(event:getCostData(self).num, skel.name)
    data.extra_data = data.extra_data or {}
    data.extra_data.lb__choumou = player.id
  end,
})

skel:addEffect(fk.BeforeCardsMove, {
  can_refresh = function(self, event, target, player, data)
    local room = player.room
    local phase = room.logic:getCurrentEvent():findParent(GameEvent.Phase)
    if phase and phase.data.extra_data and phase.data.extra_data.lb__choumou == player.id then
      for _, move in ipairs(data) do
        return move.from == player and move.skillName == "phase_discard" and move.toArea == Card.DiscardPile
            and move.moveReason == fk.ReasonDiscard
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local phase = room.logic:getCurrentEvent():findParent(GameEvent.Phase)
    if phase and phase.data.extra_data and phase.data.extra_data.lb__choumou == player.id then
      local ids = {}
      for _, move in ipairs(data) do
        if move.from == player and move.skillName == "phase_discard" and move.toArea == Card.DiscardPile
            and move.moveReason == fk.ReasonDiscard then
          local cards = table.map(move.moveInfo, function(info)
            return info.cardId
          end)
          cards = room:askToCards(player, {
            min_num = 1,
            max_num = #cards,
            cancelable = true,
            skill_name = skel.name,
            prompt = "#lb__choumou_put",
            pattern = tostring(Exppattern {id = cards}),
          })
          if #cards > 0 then
            player:addToPile("lb__choumou_qibing", cards, true, skel.name, player)
            table.insertTableIfNeed(ids, cards)
          end
          -------------------------------------------------------
          if #cards > 1 then
            room.logic:getCurrentEvent():addExitFunc(function()
              local suit = {}
              local max = 0
              for i = 1, 4, 1 do
                table.insert(suit, table.filter(player:getPile("lb__choumou_qibing"), function(id)
                  return Fk:getCardById(id).suit == i
                end))
                if #suit[i] > max then max = #suit[i] end
              end
              local list = table.filter(suit, function(l)
                return #l == max
              end)
              if #list == 1 then -- 获得改弃置
                --room:moveCardTo(list, Card.PlayerHand, player, fk.ReasonJustMove, "lb__choumou", "lb__choumou_qibing", true, player)
                room:moveCardTo(list, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, "lb__choumou",
                  "lb__choumou_qibing", true, player)
              end
            end)
          end
          -------------------------------------------------------
        end
      end
      room:cancelMove(data, ids)
    end
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

skel:addLoseEffect(function(self, player, is_start)
  local cards = player:getPile(skel.name)
  player.room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, "lb__choumou", "lb__choumou_qibing",
    true, player)
end)

return skel